<?php
autoLoader();

if( false == isset( $_GET['action'] ) || false == valStr( $_GET['action'] ) ) {
	echo 'not found::';
	exit;
}

switch( strtolower( $_GET['action'] ) ) {
	case 'ward_types':
		fetchWardTypesByWardCatagoryId();
		break;
	
	case 'wards':
		fetchWardsByWardTypeId();
		break;
		
	case 'ward_catagories':
		break;
		
	default:
		break;
}

function fetchWardTypesByWardCatagoryId() {
	if( true == isset( $_GET['ward_catagory_id'] ) && true == valStr( $_GET['ward_catagory_id'] ) ) {
		$arrobjWardTypes = new WardTypes();
		
		$strSql = 'SELECT * FROM ward_types WHERE ward_catagory_id = ' . $_GET['ward_catagory_id'] . ' AND is_published = 1;';
		$arrobjWardTypes = $arrobjWardTypes->fetchSql( $strSql );
		
		$arrmixWardTypes = array();

		if( true == valStr( $arrobjWardTypes ) ) {
			foreach( $arrobjWardTypes as $objWardType ) {
				$arrmixWardType = array();
				
				$arrmixWardType['id'] 	= $objWardType->getId();
				$arrmixWardType['name'] = $objWardType->getName();
				
				$arrmixWardTypes[] = $arrmixWardType;
			}
		}
		
		if( true == valArr( $arrmixWardTypes ) ) {
			echo json_encode( $arrmixWardTypes );
		} else {
			echo json_encode( 'not found::' );
		}
		exit;
		
	} else {
		echo json_encode( 'not found::' );
		exit;
	}
}

function fetchWardsByWardTypeId() {
	if( true == isset( $_GET['ward_type_id'] ) && true == valStr( $_GET['ward_type_id'] ) ) {
		$arrobjWards = new Wards();
	
		$strSql = 'SELECT * FROM wards WHERE ward_type_id = ' . $_GET['ward_type_id'] . ' AND is_avaliable = 1;';
		$arrobjWards = $arrobjWards->fetchSql( $strSql );
	
		$arrmixWards = array();
	
		if( true == valStr( $arrobjWards ) ) {
			foreach( $arrobjWards as $objWard ) {
				$arrmixWard = array();
	
				$arrmixWard['id'] 			= $objWard->getId();
				$arrmixWard['room_number'] 	= $objWard->getRoomNumber();
	
				$arrmixWards[] = $arrmixWard;
			}
		}
	
		if( true == valArr( $arrmixWards ) ) {
			echo json_encode( $arrmixWards );
		} else {
			echo json_encode( 'not found::' );
		}
		exit;
	
	} else {
		echo json_encode( 'not found::' );
		exit;
	}
}
?>